
import org.cgfy.generator.Driver;
import org.cgfy.generator.constant.DM;
import org.cgfy.generator.constant.MySQL;
import org.cgfy.generator.constant.Oracle;
import org.cgfy.generator.models.ClassContent;
import org.cgfy.generator.models.Template;
import org.cgfy.generator.options.DatabaseOptions;
import org.cgfy.generator.options.Options;
import utils.PropertiesUtil;

import java.util.HashMap;
import java.util.Map;

public class Executor {
    //远程
    public static final String jdbcUrl = PropertiesUtil.getDbValue("jdbcUrl");
    public static final String jdbcUser = PropertiesUtil.getDbValue("jdbcUser");
    public static final String jdbcPassword = PropertiesUtil.getDbValue("jdbcPassword");
    public static final String tableName =  PropertiesUtil.getDbValue("tableName");
    public static final String javaFileName =  PropertiesUtil.getDbValue("javaFileName");
    public static final String packageUrl = PropertiesUtil.getDbValue("packageUrl");
    public static final String databaseType = PropertiesUtil.getDbValue("databaseType");
    public static void main(String[] args){
        try {
            String prePackageUrl="com.ibp.";//前缀包路径(不变)
            String javaTemplatePath="mybatis";//(不变)
            Map<String, Template> templates = new HashMap<String, Template>();
            String outputPath=System.getProperty("user.dir");
            System.setProperty("user.dir",System.getProperty("user.dir")+"/templates");
            //java生成
            templates.put("entity", new Template()
                    .setPath(javaTemplatePath)
                    .setFile("EntityLombok.ftl")//Lombok模板为EntityLombok.ftl
                    .setPackageName(prePackageUrl+packageUrl+".domain")
                    .setClassName(javaFileName));
            templates.put("dao", new Template()
                    .setPath(javaTemplatePath)
                    .setFile("Dao.ftl")
                    .setPackageName(prePackageUrl+packageUrl+".dao")
                    .setClassName(javaFileName + "DAO"));
            templates.put("mapper", new Template()
                    .setPath(javaTemplatePath)
                    .setFile("MySqlMapper.ftl")
                    .setPackageName("dao."+packageUrl)
                    .setSourceFolder("src/main/resources")
                    .setClassName(javaFileName + "DAO")
                    .setSuffix(".xml")
            );
            templates.put("service", new Template()
                    .setPath(javaTemplatePath)
                    .setFile("Service.ftl")
                    .setPackageName(prePackageUrl+packageUrl+".service")
                    .setClassName(javaFileName + "Service"));
            templates.put("serviceImpl", new Template()
                    .setPath(javaTemplatePath)
                    .setFile("ServiceImpl.ftl")
                    .setPackageName(prePackageUrl+packageUrl+".service"+".impl")
                    .setClassName(javaFileName + "ServiceImpl"));
            templates.put("controller", new Template()
                    .setPath(javaTemplatePath)
                    .setFile("Controller.ftl")
                    .setPackageName(prePackageUrl+packageUrl+".controller")
                    .setClassName(javaFileName + "Controller"));


            //页面生成
            String pageTemplatePath="page";//(不变)
            templates.put("add", new Template()
                    .setPath(pageTemplatePath)
                    .setPackageName(pageTemplatePath)
                    .setSourceFolder("src/main/resources")
                    .setFile("Add.ftl")
                    .setClassName("addModule")
                    .setSuffix(".vue")
            );
            templates.put("modify", new Template()
                    .setPath(pageTemplatePath)
                    .setPackageName(pageTemplatePath)
                    .setSourceFolder("src/main/resources")
                    .setFile("Edit.ftl")
                    .setClassName("editModule")
                    .setSuffix(".vue")
            );
            templates.put("disp", new Template()
                    .setPath(pageTemplatePath)
                    .setPackageName(pageTemplatePath)
                    .setSourceFolder("src/main/resources")
                    .setFile("Disp.ftl")
                    .setClassName("dispModule")
                    .setSuffix(".vue")
            );
            templates.put("list", new Template()
                    .setPath(pageTemplatePath)
                    .setPackageName(pageTemplatePath)
                    .setSourceFolder("src/main/resources")
                    .setFile("Index.ftl")
                    .setClassName("index")
                    .setSuffix(".vue")
            );
            templates.put("api", new Template()
                    .setPath(pageTemplatePath)
                    .setPackageName(pageTemplatePath)
                    .setSourceFolder("src/main/resources")
                    .setFile("Api.ftl")
                    .setClassName(packageUrl)
                    .setSuffix(".js")
            );
            templates.put("expand", new Template()
                    .setPath(pageTemplatePath)
                    .setPackageName(pageTemplatePath)
                    .setSourceFolder("src/main/resources")
                    .setFile("Expand.ftl")
                    .setClassName("expand")
                    .setSuffix(".vue")
            );


            DatabaseOptions databaseOptions=null;
            // 数据库连接信息
            if("mysql".equals(databaseType)){
                databaseOptions = new DatabaseOptions(MySQL.MYSQL_8_DERVER, jdbcUrl,jdbcUser,jdbcPassword);
            }else if ("oracle".equals(databaseType)){
                databaseOptions = new DatabaseOptions(Oracle.ORACLE_DERVER, jdbcUrl);
                databaseOptions.setUser(jdbcUser);
                databaseOptions.setPassword(jdbcPassword);
            }else if ("dm".equals(databaseType)){
                databaseOptions = new DatabaseOptions(DM.DM_DERVER, jdbcUrl);
                databaseOptions.setUser(jdbcUser);
                databaseOptions.setPassword(jdbcPassword);
            }
            // 初始化执行工具
            Driver screwDriver = Driver.instance(new Options(databaseOptions).setOutputPath(outputPath+"/GENERATE/"));
            // 读取表信息
            ClassContent content = screwDriver.readTable(tableName);
            // 创建代码
            screwDriver.createCode(content,templates);
            System.out.println("生成成功!");
        }catch (Exception e){
            System.out.println("生成失败,错误信息:"+e.getMessage());
            e.printStackTrace();
        }

    }
}
